package com.dss.auto_deploy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dss.auto_deploy.domain.AdmUser;
import com.dss.auto_deploy.dto.AdmUserAddDto;
import com.dss.auto_deploy.dto.AdmUserPageDto;
import com.dss.auto_deploy.dto.AdmUserUpdateDto;
import com.dss.auto_deploy.mapper.AdmUserMapper;
import com.dss.auto_deploy.service.IAdmUserService;
import com.dss.auto_deploy.util.BeanUtils;
import com.dss.auto_deploy.util.JacksonUtils;
import com.dss.auto_deploy.util.StringUtils;
import com.dss.auto_deploy.vo.AdmUserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
* @Description:项目成员配置Service实现类
* @author dongshoushan
* @date 2022-11-13 15:11:32
*/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdmUserServiceImpl  extends ServiceImpl<AdmUserMapper,AdmUser> implements IAdmUserService {

    /**
    *
    * 项目成员配置Mapper
    */
    private final AdmUserMapper admUserMapper;
    /**
    * 新增项目成员配置
    * @param admUserAddDto
    * @return Long
    */
    @Override
    public int insert(AdmUserAddDto admUserAddDto){
        AdmUser admUser = BeanUtils.copy(admUserAddDto, AdmUser.class);
        admUser.setCreateTime(new Date());
        int insert = admUserMapper.insert(admUser);
        if (insert == 1) {
            log.info("新增成功{}",JacksonUtils.toString(admUser));
        }
        return insert;
    }


    /**
    * 修改项目成员配置
    * @param admUserUpdateDto
    * @return Long
    */
    @Override
    public int update(AdmUserUpdateDto admUserUpdateDto){
        AdmUser admUser = BeanUtils.copy(admUserUpdateDto, AdmUser.class);
        int number = admUserMapper.updateById(admUser);
        if (number == 1) {
            log.info("修改成功{}",JacksonUtils.toString(admUser));
        }
        return number;
    }


    /**
    * 批量删除项目成员配置
    * @param ids
    * @return Long
    */
    @Override
    public void deleteBatch(Long[] ids){
        this.removeByIds(Arrays.asList(ids));
    }

    /**
    * 项目成员配置分页查询
    *
    * @param pageDto
    * @return List<AdmUser>
    */
    @Override
    public Page<AdmUserVo> list(AdmUserPageDto pageDto){
        Page<AdmUser> tablesPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        LambdaQueryWrapper<AdmUser> queryWrapper = Wrappers.lambdaQuery(AdmUser.class);
        if (StringUtils.isNotEmpty(pageDto.getContent())){
            queryWrapper.like(AdmUser::getRealName,pageDto.getContent());
        }
        Page<AdmUser> pageSelectResult = admUserMapper.selectPage(tablesPage, queryWrapper);
        List<AdmUser> records = pageSelectResult.getRecords();
        List<AdmUserVo> voList = BeanUtils.copyList(records, AdmUserVo.class);
        Page<AdmUserVo> page = new Page<>();
        page.setRecords(voList);
        page.setTotal(pageSelectResult.getTotal());
        page.setSize(pageSelectResult.getSize()); 
        return page;
    }

}
